package com.brand.data.util;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

/**
 * @author scl
 * @date 2018年4月11日 下午2:48:26
 * @version 1.0
 * @description 操作Cookie的工具类
 */
public class CookieUtil {
	private static Map<String,Cookie> cookiesMap = new HashMap<>();
	private static File file;
	static{
		file = new File("cookie.txt");
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		readCookie();
	}
	public static void saveCookie(String brandName, Map<String, String> cookies) {
		Cookie cookie = cookiesMap.get(brandName);
		Cookie newCookie = new Cookie(cookies);
		if (cookie != null) {
			newCookie.setVersion(++cookie.version);
		}
		cookiesMap.put(brandName, newCookie);
		writeCookie();
	}


	public static void clearCookie(String brandName) {
		cookiesMap.remove(brandName);
	}
	public static void clearCookie(String brandName,int version) {
		Cookie cookie = getCookie(brandName);
		if (cookie != null && version == cookie.version) {
			cookiesMap.remove(brandName);
		}
	}

	public static Map<String, String> getCookies(String brandName) {
		try {
			return cookiesMap.get(brandName).getCookie();
		} catch (Exception e) {
		}
		return null;
	}
	public static Cookie getCookie(String brandName) {
		return cookiesMap.get(brandName);
	}

	public static void addCookies(String brandName, Map<String, String> cookies) {
		Cookie cookie = getCookie(brandName);
		cookie.addCookies(cookies);
		writeCookie();
	}
	public static void addKey(String brandName, String name,String value) {
		Cookie cookie = getCookie(brandName);
		cookie.addKey(name, value);
		writeCookie();
	}
	private static void writeCookie() {
		try {
			FileUtils.writeStringToFile(file, JSON.toJSONString(cookiesMap),Charset.defaultCharset());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private static void readCookie() {
		try {
			String cookieStr = FileUtils.readFileToString(file, Charset.defaultCharset());
			if (StringUtils.isNotEmpty(cookieStr)) {
				Map<String,Object> map = JSON.parseObject(cookieStr, cookiesMap.getClass());
				for (String key : map.keySet()) {
					Cookie cookie = JSON.parseObject(map.get(key).toString(), Cookie.class);
					cookiesMap.put(key,cookie);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static class Cookie{
		private Map<String, String> cookie;
		private int version;
		
		public Cookie() {}
		public Cookie(Map<String, String> cookie) {
			this.cookie = cookie;
		}
		public Map<String, String> getCookie() {
			return cookie;
		}
		public void setCookie(Map<String, String> cookie) {
			this.cookie = cookie;
		}
		public int getVersion() {
			return version;
		}
		public void setVersion(int version) {
			this.version = version;
		}
		public void addCookies(Map<String, String> cookies) {
			if (this.cookie == null) {
				this.cookie=cookies;
			}else {
				this.cookie.putAll(cookies);
			}
			this.version+=1;
		}
		public void addKey(String name,String value) {
			if (this.cookie == null) {
				this.cookie=new HashMap<>();
			}
			this.cookie.put(name, value);
			this.version+=1;
		}
		public boolean containsKey(String name) {
			return this.cookie != null && this.cookie.containsKey(name);
		}
		@Override
		public String toString() {
			return "Cookie [cookie=" + cookie + ", version=" + version + "]";
		}
	}
}
